import { OrgModel } from '@/api/manager/organize/interface'
import { useOrgTreeStore } from './blocks/main/organizeTree/store'
import { OrgTreeModel } from './blocks/main/organizeTree/translator'
import { getItemInOrgTree, removeUserInOrgTree } from './blocks/main/organizeTree/utils'
import { useParamsStore } from './blocks/main/store'
export const orgItemCheck = (item: OrgTreeModel) => {
	const tree = useOrgTreeStore.getState().tree
	getItemInOrgTree(tree, (x) => {
		x.status.isChecked = false
	})
	item.status.isChecked = true
	useOrgTreeStore.setState({ tree: [...tree] })
	useParamsStore.setState({
		currentOrgId: item.id,
	})
}
export const orgItemRemove = (removeMt: OrgModel | undefined) => {
	const prevTree = useOrgTreeStore.getState().tree
	const currentOrgId = useParamsStore.getState().currentOrgId
	let tree = prevTree
	if (removeMt) {
		tree = removeUserInOrgTree(prevTree, removeMt)
	}
	let isRemoved = true //是否已经被移除了
	getItemInOrgTree(tree, (item) => {
		if (item.id === currentOrgId) {
			isRemoved = false
		}
	})
	if (isRemoved) {
		useParamsStore.setState({
			currentOrgId: 'all',
		})
	}
	useOrgTreeStore.setState({
		tree: [...tree],
	})
}
